package com.mh.dao;

import com.mh.domain.AppUser;
import com.mh.domain.Customer;
import com.mh.domain.Invoice;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Invoice entities.
 * 
 */
@Repository("InvoiceDAO")
@Transactional
public class InvoiceDAOImpl extends AbstractJpaDao<Invoice> implements
		InvoiceDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 * 
	 * @generated
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Invoice.class }));
	/**
	 * EntityManager injected by Spring for persistence unit mhtest
	 *
	 * 
	 * @generated
	 */
	@PersistenceContext(unitName = "mhtest")
	private EntityManager entityManager;

	/**
	 * Instantiates a new InvoiceDAOImpl
	 *
	 * 
	 * @generated
	 */
	public InvoiceDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 * 
	 * @generated
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 * 
	 * @generated
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findInvoiceByDocument
	 *
	 */
	@Transactional
	public Set<Invoice> findInvoiceByDocument(String document) throws DataAccessException {

		return findInvoiceByDocument(document, -1, -1);
	}

	/**
	 * JPQL Query - findInvoiceByDocument
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Invoice> findInvoiceByDocument(String document, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findInvoiceByDocument", startResult, maxRows, document);
		return new LinkedHashSet<Invoice>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllInvoices
	 *
	 */
	@Transactional
	public Set<Invoice> findAllInvoices() throws DataAccessException {

		return findAllInvoices(-1, -1);
	}

	/**
	 * JPQL Query - findAllInvoices
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Invoice> findAllInvoices(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllInvoices", startResult, maxRows);
		return new LinkedHashSet<Invoice>(query.getResultList());
	}

	/**
	 * JPQL Query - findInvoiceByDocumentContaining
	 *
	 */
	@Transactional
	public Set<Invoice> findInvoiceByDocumentContaining(String document) throws DataAccessException {

		return findInvoiceByDocumentContaining(document, -1, -1);
	}

	/**
	 * JPQL Query - findInvoiceByDocumentContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Invoice> findInvoiceByDocumentContaining(String document, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findInvoiceByDocumentContaining", startResult, maxRows, document);
		return new LinkedHashSet<Invoice>(query.getResultList());
	}

	/**
	 * JPQL Query - findInvoiceByModifiedOn
	 *
	 */
	@Transactional
	public Set<Invoice> findInvoiceByModifiedOn(java.util.Date modifiedOn) throws DataAccessException {

		return findInvoiceByModifiedOn(modifiedOn, -1, -1);
	}

	/**
	 * JPQL Query - findInvoiceByModifiedOn
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Invoice> findInvoiceByModifiedOn(java.util.Date modifiedOn, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findInvoiceByModifiedOn", startResult, maxRows, modifiedOn);
		return new LinkedHashSet<Invoice>(query.getResultList());
	}

	/**
	 * JPQL Query - findInvoiceByCreatedBy
	 *
	 */
	@Transactional
	public Set<Invoice> findInvoiceByCreatedBy(String createdBy) throws DataAccessException {

		return findInvoiceByCreatedBy(createdBy, -1, -1);
	}

	/**
	 * JPQL Query - findInvoiceByCreatedBy
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Invoice> findInvoiceByCreatedBy(String createdBy, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findInvoiceByCreatedBy", startResult, maxRows, createdBy);
		return new LinkedHashSet<Invoice>(query.getResultList());
	}

	/**
	 * JPQL Query - findInvoiceByPrimaryKey
	 *
	 */
	@Transactional
	public Invoice findInvoiceByPrimaryKey(Integer id) throws DataAccessException {

		return findInvoiceByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findInvoiceByPrimaryKey
	 *
	 */

	@Transactional
	public Invoice findInvoiceByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findInvoiceByPrimaryKey", startResult, maxRows, id);
			return (com.mh.domain.Invoice) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findInvoiceById
	 *
	 */
	@Transactional
	public Invoice findInvoiceById(Integer id) throws DataAccessException {

		return findInvoiceById(id, -1, -1);
	}

	/**
	 * JPQL Query - findInvoiceById
	 *
	 */

	@Transactional
	public Invoice findInvoiceById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findInvoiceById", startResult, maxRows, id);
			return (com.mh.domain.Invoice) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findInvoiceByTotal
	 *
	 */
	@Transactional
	public Set<Invoice> findInvoiceByTotal(java.lang.Double total) throws DataAccessException {

		return findInvoiceByTotal(total, -1, -1);
	}

	/**
	 * JPQL Query - findInvoiceByTotal
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Invoice> findInvoiceByTotal(java.lang.Double total, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findInvoiceByTotal", startResult, maxRows, total);
		return new LinkedHashSet<Invoice>(query.getResultList());
	}

	/**
	 * JPQL Query - findInvoiceByCreatedByContaining
	 *
	 */
	@Transactional
	public Set<Invoice> findInvoiceByCreatedByContaining(String createdBy) throws DataAccessException {

		return findInvoiceByCreatedByContaining(createdBy, -1, -1);
	}

	/**
	 * JPQL Query - findInvoiceByCreatedByContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Invoice> findInvoiceByCreatedByContaining(String createdBy, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findInvoiceByCreatedByContaining", startResult, maxRows, createdBy);
		return new LinkedHashSet<Invoice>(query.getResultList());
	}

	/**
	 * JPQL Query - findInvoiceByCreatedOn
	 *
	 */
	@Transactional
	public Set<Invoice> findInvoiceByCreatedOn(java.util.Date createdOn) throws DataAccessException {

		return findInvoiceByCreatedOn(createdOn, -1, -1);
	}

	/**
	 * JPQL Query - findInvoiceByCreatedOn
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Invoice> findInvoiceByCreatedOn(java.util.Date createdOn, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findInvoiceByCreatedOn", startResult, maxRows, createdOn);
		return new LinkedHashSet<Invoice>(query.getResultList());
	}

	/**
	 * JPQL Query - findInvoiceByModifiedByContaining
	 *
	 */
	@Transactional
	public Set<Invoice> findInvoiceByModifiedByContaining(String modifiedBy) throws DataAccessException {

		return findInvoiceByModifiedByContaining(modifiedBy, -1, -1);
	}

	/**
	 * JPQL Query - findInvoiceByModifiedByContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Invoice> findInvoiceByModifiedByContaining(String modifiedBy, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findInvoiceByModifiedByContaining", startResult, maxRows, modifiedBy);
		return new LinkedHashSet<Invoice>(query.getResultList());
	}

	/**
	 * JPQL Query - findInvoiceByModifiedBy
	 *
	 */
	@Transactional
	public Set<Invoice> findInvoiceByModifiedBy(String modifiedBy) throws DataAccessException {

		return findInvoiceByModifiedBy(modifiedBy, -1, -1);
	}

	/**
	 * JPQL Query - findInvoiceByModifiedBy
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Invoice> findInvoiceByModifiedBy(String modifiedBy, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findInvoiceByModifiedBy", startResult, maxRows, modifiedBy);
		return new LinkedHashSet<Invoice>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Invoice entity) {
		return true;
	}
	
	
	@SuppressWarnings("unchecked")
	@Transactional
	public List<Invoice> findAllInvoices(Invoice invoice, int startResult, int maxRows) {
		Query query = entityManager.createQuery("select myInvoice from Invoice myInvoice");
		return new ArrayList<Invoice>(query.getResultList());
	}
	
	
	@SuppressWarnings("unchecked")
	@Transactional
	public List<Invoice> findAllInvoicesForCustomer(Customer customer, int startResult, int maxRows) {
		Query query = entityManager.createQuery("select myInvoice from Invoice myInvoice where myInvoice.customer.id = "+customer.getId());
		return new ArrayList<Invoice>(query.getResultList());
	}
	
	
	
	
	
}
